//
//  JJProgressImageView.swift
//  JJWeibo
//
//  Created by JJ on 15/9/16.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

class JJProgressImageView: UIImageView {

    var progress:CGFloat = 0 {
        didSet{
            
            progressView.progress=progress
        }
        
    }
    
    private lazy var progressView: JJProgressView = {
        let p = JJProgressView()
        
        p.backgroundColor=UIColor.clearColor()
        // 添加控件
        self.addSubview(p)
        // 设置大小
        p.frame = self.bounds
        
        return p
        }()
    
   private class JJProgressView: UIView {
        
        var progress:CGFloat = 0.0{
            didSet{
                setNeedsDisplay()
            }
        }
        
        override func drawRect(rect: CGRect) {
            
            if progress>=1{
                return
            }
            
            let center = CGPoint(x: rect.width*0.5, y: rect.height*0.5)
            let r = min(rect.width, rect.height)*0.5
            let start = -CGFloat(-M_2_PI)
            let end = 2 * CGFloat(M_PI) * progress + start
            let path=UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
            printLog(path)
            
            path.addLineToPoint(center)
            UIColor.redColor().setFill()
            path.closePath()
            path.fill()
        }
    }

}
